#include <QtGui>
#include <QtOpenGL>
#include "dice.h"

Dice::Dice(QWidget *parent) : QGLWidget(parent){
	
	setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer));

	rotationX = 0.0;
	rotationY = 0.0;
	rotationZ = 0.0;

	initializeFaceColors();
}

void Dice::initializeFaceColors(){
	faceColors[0] = Qt::red;
	faceColors[1] = Qt::blue; 
	faceColors[2] = Qt::green;
	faceColors[3] = Qt::yellow;
	faceColors[4] = Qt::white;
	faceColors[5] = Qt::gray;
}



//setar contexto de renderizacao openGL (entender melhor)
void Dice::initializeGL(){
	qglClearColor(Qt::black);
	glShadeModel(GL_FLAT);
	glEnable(GL_DEPTH_TEST);// habilita a remoção de superfícies escondidas. 
	glEnable(GL_CULL_FACE);
} 

//setar outras configuracoes q dependem do tamanho  do widget (entender melhor)
void Dice::resizeGL(int width, int height)
{
	glViewport(0, 0, width, height);//localizaçao da janela de visualizaçao
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	GLfloat x = GLfloat(width) / height;
	glFrustum(-x, +x, -1.0, +1.0, 4.0, 15.0); // define o volume visível da projeção perspectiva
	glMatrixMode(GL_MODELVIEW);
}

void Dice::paintGL()
{
	// Clear Screen And Depth Buffer	
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	draw();
}

void Dice::draw(){
	
	//TOP
	static const GLfloat V1[3] = { +1.0, +1.0, -1.0 }; // Top Right
	static const GLfloat V2[3] = { -1.0, +1.0, -1.0 }; //Top Left
	static const GLfloat V3[3] = { -1.0, +1.0, +1.0 }; //Bottom Left
	static const GLfloat V4[3] = { +1.0, +1.0, +1.0 };   //Bottom Right
	//BOTTOM
	static const GLfloat V5[3] = { +1.0, -1.0, -1.0 };
	static const GLfloat V6[3] = { -1.0, -1.0, -1.0 };
	static const GLfloat V7[3] = { -1.0, -1.0, +1.0 };
	static const GLfloat V8[3] = { +1.0, -1.0, +1.0 };
	
	//faces
	static const GLfloat * const coords[6][4] = {
        	{ V1, V2, V3, V4 }, {V8, V7, V6, V5},
		{ V1, V5, V6, V2 }, {V2, V6, V7, V3}, {V3, V7, V8, V4}, {V4, V8, V5, V1}
    	};

	// ====== ENTENDER ESSES METODOS ======
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity(); // Reset The Current Modelview Matrix
	glTranslatef(0.0, 0.0, -12.0);
	glRotatef(rotationX, 1.0, 0.0, 0.0);
	glRotatef(rotationY, 0.0, 1.0, 0.0);
	glRotatef(rotationZ, 0.0, 0.0, 1.0);

	for(int i = 0; i < 6; ++i){
		
		glLoadName(i);

		glBegin(GL_QUADS);	
		qglColor(faceColors[i]);        	
		for (int j = 0; j < 4; ++j) {
			glVertex3f(coords[i][j][0], coords[i][j][1], coords[i][j][2]);	      		
		}
	        glEnd();		
	}
}


void Dice::mousePressEvent(QMouseEvent *event)
{
    lastPos = event->pos();
}

void Dice::posChanged(QPoint pos)
{
    lastPos = pos;
}

void Dice::mouseMoveEvent(QMouseEvent *event)
{
    GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
    GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();
    if (event->buttons() & Qt::LeftButton) {
        rotationX += 180 * dy;
        rotationY += 180 * dx;
        updateGL();
    }
    lastPos = event->pos();
}

void Dice::tuioCursor(int touchId, double x, double y, int type)
{

    int y2 = this->height() * y;
    int x2 = this->width() * x;
    QPoint pos =QPoint(x2,y2);
    GLfloat dx = GLfloat(x2 - lastPos.x()) / width();
    GLfloat dy = GLfloat(y2 - lastPos.y()) / height();
    if (type == 2) {
        rotationX += 180 * dy;
        rotationY += 180 * dx;
        updateGL();
    }
    lastPos = pos;
    posChanged(pos);
}
